-- 按键驱动程序

module(..., package.seeall)
require "pins"
-- require "config"
pmd.ldoset(15, pmd.LDO_VMMC)


--记录上次上升沿的时间
local BTN_ENTRY_NEGEDGE = -1
local BTN_RETURN_NEGEDGE = -1
local BTN_DOWN_NEGEDGE = -1

local sleep_count = 0
local over_time = 998

-----------------------------------3个按键注册函数-----------------------------------
local function KeyReturnFnc(msg)
    if msg == cpu.INT_GPIO_NEGEDGE then
        BTN_RETURN_NEGEDGE = rtos.tick() --ticks，时间计数，每 tick 时长5ms
    elseif msg == cpu.INT_GPIO_POSEDGE then
        local curtick = rtos.tick()

        if curtick - BTN_RETURN_NEGEDGE > 120 then
            log.info("return key is long press")
            if sleep_count > over_time then
                disp.sleep(0)
                pmd.ldoset(15, pmd.LDO_VLCD)
                sleep_count = 0
            else
                sys.publish("KEY_EVEN", "return_long")
            end
        else
            log.info("return key is short press")
            if sleep_count > over_time then
                disp.sleep(0)
                pmd.ldoset(15, pmd.LDO_VLCD)
                sleep_count = 0
            else
                sys.publish("KEY_EVEN", "return")
            end

        end
        BTN_DOWN_NEGEDGE = -1
    end
end

local function KeyDownFnc(msg)
    if msg == cpu.INT_GPIO_NEGEDGE then
        BTN_DOWN_NEGEDGE = rtos.tick() --ticks，时间计数，每 tick 时长5ms
    elseif msg == cpu.INT_GPIO_POSEDGE then
        local curtick = rtos.tick()

        if curtick - BTN_DOWN_NEGEDGE > 120 then
            log.info("down key is long press")
            if sleep_count > over_time then
                disp.sleep(0)
                pmd.ldoset(15, pmd.LDO_VLCD)
                sleep_count = 0
            else
                sys.publish("KEY_EVEN", "down_long")
            end
        else
            log.info("down key is short press")
            if sleep_count > over_time then
                disp.sleep(0)
                pmd.ldoset(15, pmd.LDO_VLCD)
                sleep_count = 0
            else
                sys.publish("KEY_EVEN", "down")
            end
        end
        BTN_DOWN_NEGEDGE = -1
    end
end

local function KeyEnterFnc(msg)
    if msg == cpu.INT_GPIO_NEGEDGE then
        BTN_ENTRY_NEGEDGE = rtos.tick() --ticks，时间计数，每 tick 时长5ms
    elseif msg == cpu.INT_GPIO_POSEDGE then
        local curtick = rtos.tick()

        if curtick - BTN_ENTRY_NEGEDGE > 120 then
            log.info("enter key is long press")
            if sleep_count > over_time then
                disp.sleep(0)
                pmd.ldoset(15, pmd.LDO_VLCD)
                sleep_count = 0
            else
                sys.publish("KEY_EVEN", "enter_long")
            end
        else
            log.info("enter key is short press")
            if sleep_count > over_time then
                disp.sleep(0)
                pmd.ldoset(15, pmd.LDO_VLCD)
                sleep_count = 0
            else
                sys.publish("KEY_EVEN", "enter")
            end

        end
        BTN_ENTRY_NEGEDGE = -1
    end
end

sys.taskInit(function()
    local getKeyEnterFnc = pins.setup(pio.P0_11, KeyDownFnc, pio.PULLUP)
    sys.wait(1000)
    local getKeyUpFnc = pins.setup(pio.P0_10, KeyEnterFnc, pio.PULLUP)
    sys.wait(1000)
    local getKeyDownFnc = pins.setup(pio.P0_12, KeyReturnFnc, pio.PULLUP)
end)

local function lcdSleep()
    sleep_count = sleep_count + 1

    if sleep_count > over_time then
        disp.sleep(1)
        pmd.ldoset(5, pmd.LDO_VLCD)
    end

    if sleep_count > 999 then
        sleep_count = 999
    end
end

-- 定时器，5s
sys.taskInit(function()
    while true do
        sys.wait(5000)
        sys.publish("TIME_STICK")
    end
end)

sys.subscribe("TIME_STICK", lcdSleep)

